package com.newsystem.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.newsystem.MyUtils.CommonResult;
import com.newsystem.mapper.AdminMapper;
import com.newsystem.mapper.UserMapper;
import com.newsystem.pojo.Admin;
import com.newsystem.pojo.User;
import com.newsystem.pojo.vo.AdminInfo2;
import com.newsystem.pojo.vo.GeneralUserInfo;
import com.newsystem.service.impl.AdminService;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 管理员 前端控制器
 * </p>
 *
 * @author chenlitao
 * @since 2022-11-06
 */
@RestController
@RequestMapping("/admin")
@RequiredArgsConstructor
public class AdminController {

    private final AdminService adminService;
    private final UserMapper userMapper;
    private final AdminMapper adminMapper;



    @ApiOperation("管理员注册")
    @PutMapping("/register")
    public CommonResult<Object> register(User user,Admin admin){
        if(user.getAccount() == null){
            return CommonResult.failed("用户账号不能为空");
        }
        if(user.getPassword() == null){
            return CommonResult.failed("用户密码不能为空");
        }
        if(user.getRole() == 0){
            return adminService.insertOneAdmin(user,admin);
        }
        else {
            return CommonResult.failed("请确定是否申请管理员");
        }
    }

    @ApiOperation("管理员获取自己的信息")
    @GetMapping("/getinfo")
    public CommonResult<Object> getInfo(String adminUserId){
        return adminService.selectAdminInfoByAdminUserId(adminUserId);
    }

//    @ApiOperation("管理员修改自己的信息")
//    @PutMapping("/update")
//    public CommonResult<Object> update(User user,Admin admin){
//        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
//        userUpdateWrapper.eq("id",user.getId())
//                .eq("statue",1);
//        if(userMapper.update(user,userUpdateWrapper) >0) {
//            UpdateWrapper<Admin> adminUpdateWrapper = new UpdateWrapper<>();
//            adminUpdateWrapper.set("user_id",user.getId());
//            if(adminMapper.update(admin,adminUpdateWrapper) > 0){
//                return CommonResult.success("更新成功");
//            }
//        }
//        return CommonResult.failed("管理员用户信息更新失败");
//    }

    @ApiOperation("管理员修改自己的信息")
    @PutMapping("/update")
    public CommonResult<Object> update(AdminInfo2 admin){
        if(adminMapper.updateAdmin(admin) > 0){
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }

    @ApiOperation("管理员获取所有普通用户")
    @GetMapping("/getGenUsers")
    public CommonResult<Object> getGenUser(Integer startpage,Integer pagesum){
        return adminService.selectAllGeneralUser(startpage,pagesum);
    }

    @ApiOperation("管理员获取存在的普通用户")
    @GetMapping("/getGenUserExit")
    public CommonResult<Object> getGenUserExit(Integer startpage,Integer pagesum){
        return adminService.selectAllGeneralUserExit(startpage,pagesum);
    }

    @ApiOperation("管理员获取已经注销的普通用户")
    @GetMapping("/getGenUsersed")
    public CommonResult<Object> getGenUsered(Integer startpage,Integer pagesum){
        return adminService.selectAllGeneralUsered(startpage,pagesum);
    }

    @ApiOperation("管理员获取所有媒体号")
    @GetMapping("/getMediums")
    public CommonResult<Object> getMedium(Integer startpage,Integer pagesum){
        return adminService.selectAllMedium(startpage,pagesum);
    }

    @ApiOperation("管理员获取存在的媒体号")
    @GetMapping("/getMediumsExit")
    public CommonResult<Object> getGenMediumExit(Integer startpage,Integer pagesum){
        return adminService.selectAllMediumExit(startpage,pagesum);
    }

    @ApiOperation("管理员获取已经注销的媒体号")
    @GetMapping("/getMediumsed")
    public CommonResult<Object> getGenMediumed(Integer startpage,Integer pagesum){
        return adminService.selectAllMediumed(startpage,pagesum);
    }

    @ApiOperation("彻底普通用户账号")
    @DeleteMapping("/deleteGenUser")
    public CommonResult<Object> deleteGenUser(@RequestBody List<String> idList){
        int i = adminMapper.deleteGenUser(idList);
        if(i>0){
            return CommonResult.success("删除"+i/2+"条");
        }
        return CommonResult.failed("删除失败");

    }

    @ApiOperation("彻底媒体账号")
    @DeleteMapping("/deleteMedium")
    public CommonResult<Object> deleteMedium(@RequestBody List<String> idList){
        int i = adminMapper.deleteMedium(idList);
        if(i>0){
            return CommonResult.success("删除"+i/2+"条");
        }
        return CommonResult.failed("删除失败");
    }



}

